package com.xiaoniu.forum.common.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.websocket.server.ServerEndpointConfig;


@Component
public class SpringUtils extends ServerEndpointConfig.Configurator implements ApplicationContextAware {
    private static ApplicationContext context;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtils.context=applicationContext;
    }

    //根据Id从Spring容器获取Bean的方法
    public Object getBean(String beanName){
        return context.getBean(beanName);
    }

    //根据类型从Spring容器获取Bean的方法
    public <T> T getBean(Class<T> clazz){
        return (T)context.getBean(clazz);
    }

}
